package ee.graphicscard.controller;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class GraphicsCardServlet
 */
@WebServlet("/s")
public class GraphicsCardServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(GraphicsCardServlet.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GraphicsCardServlet() 
    {
        super();
    }
    
    @Override
    public void init() throws ServletException 
    {
    	super.init();
    	logger.debug("I was initialized.");
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		try
		{
			ViewManager viewManager = new ViewManager();
			ServletContext servletContext = getServletConfig().getServletContext();
			GraphicsCardMenuController graphicsCardMenuController = new GraphicsCardMenuController();
			String userView = graphicsCardMenuController.service(request, response);
			System.out.println(userView);
			viewManager.navigateTo(userView, request, response, servletContext);
	
		}
		catch (Exception ex)
		{
			logger.warn("Somekind of an error in a main servlet object (controller)");
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	@Override
    public void destroy()
	{
		logger.debug("I was destroyed.");
    }

}
